//
//  YLProjectDetailViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/9.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLProjectDetailViewController.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
#import <UIImageView+WebCache.h>
#import "YLProductListModel.h"
#import "PXAlertView.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLCertifiedPaymentViewController.h"
@interface YLProjectDetailViewController ()
@property (nonatomic, weak) UIButton *invitingButton;
@end

@implementation YLProjectDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    UILabel *contentLabel = [[UILabel alloc] init];
    [self.view addSubview:contentLabel];
    contentLabel.text = @"正在加载详情数据...";
    contentLabel.textAlignment = NSTextAlignmentCenter;
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(weakSelf.view);
    }];
    contentLabel.textColor = [UIColor lightGrayColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"项目详情";
    
    self.view.backgroundColor = YLBackGroundColor;
    
    if (!_model) {
        return;
    }
    
    UIButton *invitingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:invitingButton];
    _invitingButton = invitingButton;
    [invitingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.height.mas_equalTo(44);
        
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(weakSelf.view);
        }
    }];
    
    invitingButton.backgroundColor = YLLOrgColor;
    [invitingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [invitingButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [invitingButton setTitle:@"预约详情" forState:UIControlStateNormal];
    
    if (_model.isReservation == YES) {
        // 已预约
        [self appointmentStatus:YES];
    } else {
        [self appointmentStatus:NO];
    
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view);
        make.bottom.mas_equalTo(invitingButton.mas_top);
    }];

    UIImageView *imageView = [[UIImageView alloc] init];
    
    imageView.backgroundColor = [UIColor clearColor];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", YLImageURL, _model.projectInfoDetailImg]]];
    [scrollView addSubview:imageView];
    
    
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.right.left.mas_equalTo(scrollView);
        if (weakSelf.model.projectInfoDetailImgHeight == 0) {
            make.height.mas_equalTo(YLScreenW);
        } else {
            make.height.mas_equalTo(YLScreenW * weakSelf.model.projectInfoDetailImgHeight / weakSelf.model.projectInfoDetailImgWidth);
        }
        
        make.width.mas_equalTo(YLScreenW);
    }];
    
}


// 设置预约状态
- (void)appointmentStatus:(BOOL)status
{
    if (status == YES) {
        _invitingButton.backgroundColor = [UIColor lightGrayColor];
        [_invitingButton setTitle:@"已预约" forState:UIControlStateNormal];
        _invitingButton.userInteractionEnabled = NO;
    } else {
        _invitingButton.backgroundColor = YLLOrgColor;
        [_invitingButton setTitle:@"预约咨询" forState:UIControlStateNormal];
        _invitingButton.userInteractionEnabled = YES;
        
        // MEI YOU YU YUE
        if (_model.projectInfoState == -1) {
            [self infoStatus:NO];
        }
        
    }
}

// 是否上下线
- (void)infoStatus:(BOOL)status
{
    if (status == YES) {
        // 上线
        
    } else {
        _invitingButton.backgroundColor = [UIColor lightGrayColor];
        [_invitingButton setTitle:@"已下线" forState:UIControlStateNormal];
        _invitingButton.userInteractionEnabled = NO;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)btnClick:(UIButton *)sender
{
    __weak typeof(self) weakSelf = self;
    YLAccount *account = [YLAccountTool account];
    if (account.authState == 0) {
        YLCertifiedPaymentViewController *con = [[YLCertifiedPaymentViewController alloc] init];
        [weakSelf.navigationController pushViewController:con animated:YES];
        return ;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否进行预约?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"预约" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *url = [NSString stringWithFormat:@"%@Product/ProjectReservation", YLURL];
        [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"Id" : weakSelf.model.projectId} success:^(id responseObject) {
            if ([responseObject[@"httpCode"] integerValue] == 200) {
                
                [PXAlertView showAlertWithTitle:@"您已成功预约" message:@"您的专属理财师将会在一个工作日联系您" cancelTitle:@"知道了" completion:^(BOOL cancelled) {
                    [weakSelf appointmentStatus:YES];
                    weakSelf.model.isReservation = YES;
                }];
            } else {
                [WHProgressHUD showError:responseObject[@"message"]];
            }
        } fail:^(NSError *error) {
        }];
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
    
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}

@end
